<?php
class response
{
	private $js_func="";
	private $flag=true;
	
	public function text_alert($type,$id,$text)
	{
		$str = str_replace(array("'",'"'),array("\\\'",'\\\"'),$text);
		$this->js_func .= "text_alert(\'$type\',\'$id\',\'$str\');";
	}
	
	public function alert($str)
	{
		$this->flag=false;
		$str = str_replace(array("'",'"'),array("\\\'",'\\\"'),$str);
		$this->js_func .= "alert(\'$str\');";
	}
	
	public function redirect($str)
	{
		$this->flag=false;
		$str = str_replace(array("'",'"'),array("\\\'",'\\\"'),$str);
		$this->js_func .= "self.location.href=\'$str\'";
	}
	
	public function script($str)
	{
		$this->flag=false;
		$str = str_replace(array("'",'"'),array("\'",'\"'),$str);
		$str = str_replace('___','"',$str);
		$this->js_func .= $str;
	}
	
	/**
	 * 强制执行js
	 *
	 */
	public function real_execute()
	{
		$this->flag=false;
		if(empty($this->js_func))
		{
			return true;
		}
		$this->execute();
	}
	
	public function execute()
	{
		if($this->flag===true)
		{
			$this->js_func = "";
			return true;
		}
		$this->flag = true;
		if(!empty($this->js_func))
		{
			header("Cache-Control: no-store, no-cache, must-revalidate");
			header("Cache-Control: post-check=0, pre-check=0", false);
			header("Pragma: no-cache");
			?>
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
			<script language="javascript">
			try
			{
				parent.r_call('<?php echo $this->js_func;?>');
			}
			catch(exception)
			{
				alert(exception.message);
			}
			</script>
			</head>
			<body>
			</body>
			</html>
			<?php
			$this->js_func = "";
		}
		exit;
	}
	
}
?>